package com.demo.CountDownLatchDemo;

import java.util.concurrent.CountDownLatch;

/**
 * Created by chenhe17596 on 2017/6/24.
 */
public class Demo {
    private CountDownLatch latch ;

    public Demo(){
            latch = new CountDownLatch(2);
            t2.start();
            t3.start();
            t1.start(); // t1等t2,t3执行完成后执行
    }
    Thread t1 = new Thread(new Runnable() {
        public void run() {
           try {
               latch.await();
               System.out.println("t1 开始执行 : " + latch.getCount());
           }catch (InterruptedException e){
               e.printStackTrace();
           }
        }
    });
    Thread t2 = new Thread(new Runnable() {
        public void run() {
            synchronized (latch) {
                System.out.println("t2 开始执行 : " + latch.getCount());
                latch.countDown();
            }
        }
    });
    Thread t3 = new Thread(new Runnable() {
        public void run() {
            synchronized (latch) {
                System.out.println("t3 开始执行 : " + latch.getCount());
                latch.countDown();
            }
        }
    });

    public static void main(String[] arg) {
        new Demo();
    }
}
